﻿using Infrastructure;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Inspection.Helper;
using System.Diagnostics;
using Vision.AlgBase;
using Inspection.Models;
using System.Windows.Controls;

namespace Inspection
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : Application
    {
        public App()
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            DispatcherUnhandledException += App_DispatcherUnhandledException;
            if (ProcessIsOpen(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString(), 2))
            {
                Xceed.Wpf.Toolkit.MessageBox.Show("软件已经开启，请勿重复打开.......", "警告", MessageBoxButton.OK, MessageBoxImage.Warning, null);
                return;
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            LogServiceHelper.Register(new LogNetHelper());
            AddInContainer.Instance.LoadAddIn();
            SysInitialParame parame = SysInitialParame.Load();
            if (null != parame)
            {
                GlobalParame.MainTCPClient = new SuperSimpleTcp.SimpleTcpClient(parame.MainSWIP, parame.AlgTcpPortNum);
                try
                {
                    GlobalParame.MainTCPClient.Connect();
                }
                catch (Exception ex)
                { }
            }
        }

        protected override void OnExit(ExitEventArgs e)
        {
            base.OnExit(e);
        }


        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
            Exception ex = e.Exception;
            string errorMsg = "App_DispatcherUnhandledException : \n\n" + ex.Message + ex.StackTrace;
            MessageBox.Show(errorMsg);
            LogServiceHelper.Error(string.Format("【{0}】-【App_DispatcherUnhandledException】-【异常】:{1}", "App", errorMsg));
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception exp = (Exception)e.ExceptionObject;
            string strErr = exp.ToString();
            LogServiceHelper.Error(strErr);
            MessageBox.Show(strErr);
        }

        /// <summary>
        /// 获取硬盘信息
        /// </summary>
        /// <returns></returns>
        private List<string> GetDiskList()
        {
            string[] drivers = Environment.GetLogicalDrives();
            return new List<string>(drivers);
        }

        /// <summary>
        /// 防止程序二次启动
        /// </summary>
        /// <param name="ProcessName">程序名称</param>
        /// <param name="Counter">程序允许打开个数</param>
        /// <returns></returns>
        private bool ProcessIsOpen(string ProcessName, int Counter)
        {
            string[] ProcessNames;
            bool openFlag = false;
            Process[] pro = Process.GetProcesses();
            ProcessNames = new string[pro.Length];
            int count = 0;
            for (int i = 0; i < pro.Length; i++)
            {
                ProcessNames[i] = pro[i].ProcessName.ToString();
                if (pro[i].ProcessName.ToString().Contains(ProcessName) && !pro[i].ProcessName.ToString().Contains(".vshost"))
                {
                    count++;
                }
                if (count == Counter)
                {
                    openFlag = true;
                    break;
                }
                else
                    openFlag = false;
            }
            return openFlag;
        }
    }
}
